package com.sdp.lease.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.redis.RedisUtil;
import com.sdp.core.bizc.utils.SnowFlake;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.lease.db.l_sys_config.entity.LSysConfig;
import com.sdp.lease.db.l_sys_config.service.ILSysConfigService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-08-05
 */
@RestController
@RequestMapping("/LSysConfigController")
public class LSysConfigController {

	@Autowired
	private ILSysConfigService ilSysConfigService;

	@Autowired
	private Query query;

	@Autowired
	private UniqueCheckTools uniqueCheckTools;

	@Autowired
	private RedisUtil RedisUtil;

	@Autowired
	private SnowFlake SnowFlake;

	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(ilSysConfigService, LSysConfig.class, dataCenter));
	}

	@PostMapping(value = "/save")
	@Transactional
	public Result<?> save(@RequestBody LSysConfig sysConfig) {
		if(sysConfig==null) {
			return Result.error("系统配置信息为空");
		}
		if(StringUtils.isEmpty(sysConfig.getKeyId())) {
			sysConfig.setParamCode(SnowFlake.create64OrderNo());
			Result<?> r = uniqueCheckTools.isUniqueSingle(ilSysConfigService, LSysConfig.class, new String[] {"paramCode"}, "keyId", sysConfig, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("参数编码不能重复");
			}
			ilSysConfigService.save(sysConfig);

		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(ilSysConfigService, LSysConfig.class, new String[] {"paramCode"}, "keyId", null, sysConfig, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("参数编码不能重复");
			}
			ilSysConfigService.updateById(sysConfig);

		}

		//更新缓存
		RedisUtil.setMap(LSysConfig.class, sysConfig.getParamCode(),sysConfig.getParamVal());
		return Result.ok(sysConfig);
	}

	/**
	 * 根据参数编码获取参数值
	 * @param paramCode
	 * @return
	 */
	@PostMapping(value = "/getSysConfigByCode")
	public Result<?> getSysConfigByCode(@RequestBody String paramCode) {
		Object paramVal = RedisUtil.getMap(LSysConfig.class, paramCode);
		if(StringUtils.isEmpty(paramVal)) {
			QueryWrapper<LSysConfig> query = new QueryWrapper<>();
			query.eq("param_code", paramCode);
			LSysConfig config = ilSysConfigService.getOne(query);
			if(!StringUtils.isEmpty(config)) {
				paramVal = config.getParamVal();
				RedisUtil.setMap(LSysConfig.class, paramCode,paramVal);
			}
		}
		if(StringUtils.isEmpty(paramVal)) {
			paramVal="";
		}
		return Result.ok(paramVal);
	}
	
	
	/**
	 * 根据参数编码获取参数值
	 * @param paramCode
	 * @return
	 */
	public String getSysConfigParamValByCode(@RequestBody String paramCode) {
		Object paramVal = RedisUtil.getMap(LSysConfig.class, paramCode);
		if(StringUtils.isEmpty(paramVal)) {
			QueryWrapper<LSysConfig> query = new QueryWrapper<>();
			query.eq("param_code", paramCode);
			LSysConfig config = ilSysConfigService.getOne(query);
			if(!StringUtils.isEmpty(config)) {
				paramVal = config.getParamVal();
				RedisUtil.setMap(LSysConfig.class, paramCode,paramVal);
			}
		}
		if(StringUtils.isEmpty(paramVal)) {
			paramVal="";
		}
		return paramVal.toString();
	}
}

